package cn.gov.pbc.gd.credit.web.controller.user;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liu.jbase.PageListVo;
import com.liu.jbase.ResultVo;
import com.liu.jbase.controller.impl.BaseControllerImpl;

import cn.gov.pbc.gd.credit.common.constant.CodeConstant;
import cn.gov.pbc.gd.credit.user.service.online.IOnlineUserService;
import cn.gov.pbc.gd.credit.user.service.realuser.IRealUserService;
import cn.gov.pbc.gd.credit.user.service.user.IUserService;
import cn.gov.pbc.gd.credit.vo.online.OnlineUserVo;
import cn.gov.pbc.gd.credit.vo.realuser.RealUserVo;
import cn.gov.pbc.gd.credit.vo.realuser.ResRealUserVo;
import cn.gov.pbc.gd.credit.vo.user.ResUserVo;
import cn.gov.pbc.gd.credit.vo.user.UserVo;

@Controller
@RequestMapping("/user")	
public class UserController extends BaseControllerImpl<ResUserVo, UserVo, IUserService> {

	@Autowired
	private IUserService userService;
	
	@Autowired
	private IOnlineUserService onlineUserService;
	
	@Autowired
	private IRealUserService realUserService;
	
	private static Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@ResponseBody
	@RequestMapping("/modify/pwd")
	public ResultVo<ResUserVo> modifyPwd(UserVo vo, HttpServletRequest request){//修改用户登陆密码
		
        ResultVo<ResUserVo> result = new ResultVo<ResUserVo>();
        
		result = userService.modifyPwd(vo);
		
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/default/pwd")
	public ResultVo<ResUserVo> defaultPwd(UserVo vo, HttpServletRequest request){//恢复缺省密码
		
        ResultVo<ResUserVo> result = new ResultVo<ResUserVo>();
        
		result = userService.defaultPwd(vo, request);
		
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/modify/status")
	public ResultVo<ResUserVo> modifyStatus(UserVo vo, HttpServletRequest request){//修改用户登陆密码
		
        ResultVo<ResUserVo> result = userService.updateStatus(vo, request);
		
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/update/head/img")
	public ResultVo<String> updateHeadImg(UserVo vo, HttpServletRequest request){
		
		return userService.updateHeadImg(vo, request);
	}
	
	@RequestMapping("/cert/head/img")
	public String certHeadImg(UserVo vo, Model model, HttpServletRequest request){
		
        model.addAttribute("id", vo.getId());
		
		return "user/certHeadImg";
	}
	
	@RequestMapping("/head/img/view")
	public String headImgView(UserVo vo, Model model, HttpServletRequest request){
		
        model.addAttribute("id", vo.getId());
		
		return "user/modifyHead";
	}
	
	@RequestMapping("/online/page")
	public String online(UserVo vo,  Model model, HttpServletRequest request){
		
		ResultVo<PageListVo<OnlineUserVo>> resultVo = onlineUserService.page(vo, request);
		model.addAttribute("result", resultVo);		
		return "user/online";
	}
	
	@ResponseBody
	@RequestMapping("/sign/name")
	public ResultVo<String> userSign(UserVo vo, HttpServletRequest request){//用户签名
		
        ResultVo<String> result = userService.userSign(vo, request);
		
		return result;
	}
	
	@RequestMapping("/sign/page")
	public String userPage(UserVo vo, Model model, HttpServletRequest request){//用户签名
		
        model.addAttribute("result", userService.find(vo, request));
		
		return "user/signName";
	}
	
	@ResponseBody
	@RequestMapping("/query/operater/list")
	public ResultVo<List<ResUserVo>> queryOperaterList(UserVo vo, HttpServletRequest request){//用户签名
		
		ResultVo<List<ResUserVo>> result = userService.queryOperaterList(vo, request);
		
		return result;
	}
	
	/**
	 * 修改虚拟用户
	 * @param userVo
	 * @param request
	 * @return 
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/updateUser")
	public ResultVo<ResUserVo> updateRealUser(UserVo userVo, HttpServletRequest request) throws Exception{
		//realUserService.modifyPwdInFixedTime(userVo,request);
		if(userVo.getRealUserList()!=null&&userVo.getRealUserList()!=""){
			String realUserId=userVo.getRealUserList();
			String realUserWithOutUserList=userVo.getRealUserWithOutUserList();
			String[]realUserArray=realUserId.split(";");
			String[] realUserWithOutUserArray={};
			if(realUserWithOutUserList!=null){
				realUserWithOutUserArray=realUserWithOutUserList.split(";");
			}
			RealUserVo realUserVo=null;
			for(int g=1;g<realUserArray.length;g++){//更新真实用户的userId(将其设置为当前的虚拟用户的userId)
				realUserVo=new RealUserVo();
				realUserVo.setId(realUserArray[g]);
				realUserVo.setUserId(userVo.getId());
				realUserService.updateRealUser(realUserVo,request);
			}
			for(int c=1;c<realUserWithOutUserArray.length;c++){//更新真实用户的userId(将其设置为"")
				realUserVo=new RealUserVo();
				realUserVo.setId(realUserWithOutUserArray[c]);
				realUserVo.setUserId("");
				realUserService.updateRealUser(realUserVo,request);
			}
			String realUserIdInUser="";
			for(int g=1;g<realUserArray.length;g++){//获取用户类型为查询员的真实用户id
				ResultVo<ResRealUserVo>resultVo=realUserService.findById(realUserArray[g], request);
				Integer userType=resultVo.getData().getUserType();
				if(userType==1){
					realUserIdInUser=resultVo.getData().getId();
					break;
				}
			}
			userVo.setRealUserId(realUserIdInUser);
		}
		/**
		 * 给角色权限赋值
		 */
		String roleString =userVo.getRoleString();
		List<String> roleIdList=new ArrayList<String>();
		if(roleString!=null&&roleString!=""){
			String [] roleArray=roleString.split(";");
			for(int v=1;v<roleArray.length;v++){
				roleIdList.add(roleArray[v]);
			}
		}
		userVo.setRoleIdList(roleIdList);
		return super.update(userVo, request);
	}
	@ResponseBody
	@RequestMapping("/addUser")
	public void addUser(UserVo userVo, HttpServletRequest request) throws Exception{
		/**
		 * 给角色权限赋值
		 */
		String roleString =userVo.getRoleString();
		List<String> roleIdList=new ArrayList<String>();
		if(roleString!=null&&roleString!=""){
			String [] roleArray=roleString.split(";");
			for(int v=1;v<roleArray.length;v++){
				roleIdList.add(roleArray[v]);
			}
		}
		userVo.setRoleIdList(roleIdList);
		if(userVo.getRealUserList()==null||userVo.getRealUserList()==""){
			super.save(userVo, request);
		}else{
			RealUserVo realUserVo=null;
			String realUserId=userVo.getRealUserList();
			String[]realUserArray=realUserId.split(";");
			String realUserIdInUser="";
			for(int g=1;g<realUserArray.length;g++){//获取用户类型为查询员的真实用户id
				ResultVo<ResRealUserVo>resultVo=realUserService.findById(realUserArray[g], request);
				Integer userType=resultVo.getData().getUserType();
				if(userType==1||userType==3){
					realUserIdInUser=resultVo.getData().getId();
					break;
				}
			}
			userVo.setRealUserId(realUserIdInUser);
			super.save(userVo, request);
			for(int g=1;g<realUserArray.length;g++){//更新真实用户的userId(将其设置为当前的虚拟用户的userId)
				realUserVo=new RealUserVo();
				realUserVo.setId(realUserArray[g]);
				realUserVo.setUserId(userVo.getId());
				realUserService.updateRealUser(realUserVo,request);
			}
		}
	}
	/**
	 * 虚拟用户的详情信息展现
	 * @param id
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getUserDetail")
	public 	String getUserDetail(String id,HttpServletRequest request,Model model) throws Exception{
		ResultVo<List> realUserResult=realUserService.queryRealUserByUserId(id);//获取当前虚拟用户关联的真实用户的信息
		String realUserName="";//真实用户姓名
		if(realUserResult.getData()!=null){
			ArrayList<ResRealUserVo> resRealUserVoList=(ArrayList<ResRealUserVo>) realUserResult.getData();
			for(int d=0;d<resRealUserVoList.size();d++){
				String userName=resRealUserVoList.get(d).getUserName();
				if(d==0){
					realUserName=userName;
				}else{
					realUserName=realUserName+","+userName;
				}
			}
		}
		UserVo userVo=new UserVo();
		userVo.setId(id);
		ResultVo<ResUserVo> userResult=super.detail(userVo, request);//当前虚拟用户的详细信息
		userResult.getData().setRealUserName(realUserName);
		model.addAttribute("result",userResult);
		return "user/detail";
	}
	
}
